package com.yhp.service.impl;

import com.yhp.bean.Menu;
import com.yhp.dao.MenuDao;
import com.yhp.dao.impl.MenuDaoImpl;
import com.yhp.service.MenuService;

import java.util.ArrayList;
import java.util.List;

public class MenuServiceImpl implements MenuService {
    private MenuDao dao=new MenuDaoImpl();
    @Override
    public List<Menu> getMenuList() {
        List<Menu> menuList = dao.getMenuList();//未分一二级
        return getMenus(menuList);
    }

    @Override
    public List<Menu> getMenuList(int pageIndex, int pageSize) {
        return new MenuDaoImpl().getMenuList(pageIndex,pageSize);
    }

    @Override
    public int total() {
        return new  MenuDaoImpl().total();
    }

    @Override
    public List<Menu> getMenuList(int roleId) {
        List<Menu> menuList = dao.getMenuList(roleId);
        return getMenus(menuList);
    }

    /***
     * 分级方法
     * @param menuList
     * @return
     */
    private List<Menu> getMenus(List<Menu> menuList) {
        List<Menu> newMenuList=new ArrayList<>();//保存分级以后的菜单
        for (Menu menu : menuList) {
            if(menu.getUpmenuId()==0){ //说明是一级菜单
                List<Menu> secondList = new ArrayList<>();
                for (Menu second : menuList) {
                    if(second.getUpmenuId()==menu.getMenuId()){
                        secondList.add(second);
                    }
                }
                menu.setSecondMenuList(secondList);
                newMenuList.add(menu);
            }
        }
        return newMenuList;
    }
}
